package org.zoomdev.zoom.web.rendering.impl;

import org.zoomdev.zoom.web.action.ActionContext;
import org.zoomdev.zoom.web.rendering.Rendering;
import org.zoomdev.zoom.web.rendering.ThrowableRenderProcess;

import java.io.IOException;

public abstract class BaseRendering implements Rendering {

    private ThrowableRenderProcess throwableRenderProcess;

    public ThrowableRenderProcess getThrowableRenderProcess() {
        return throwableRenderProcess;
    }

    public void setThrowableRenderProcess(ThrowableRenderProcess throwableRenderProcess) {
        this.throwableRenderProcess = throwableRenderProcess;
    }

    @Override
    public boolean render(ActionContext context) throws Exception {
        Object result = context.getRenderObject();
        if (result instanceof Throwable) {
            Throwable exception = (Throwable) result;
            if (throwableRenderProcess != null && throwableRenderProcess.process(context, exception)) {
                return true;
            }
            exception = context.getException();
            processThrowable(context, exception);
        } else {
            processNormal(context);
        }
        return true;

    }


    protected abstract void processNormal(ActionContext context) throws Exception;

    protected abstract void processThrowable(ActionContext context, Throwable throwable) throws IOException;
}
